import { defineStore } from 'pinia'
import type { IState } from './type'
import { selectListApi } from '@/service/api'
import type { selectList } from '@/service/api/topbar/type'
import { iscode } from '@/utils'

const useTopbarStore = defineStore('topbar', {
  state: (): IState => ({
    tabs: [
      {
        name: 'article',
        label: '文章'
      },
      {
        name: 'appspace',
        label: 'App Space'
      },
      {
        name: 'aimodel',
        label: 'AI模型'
      },
      {
        name: 'document',
        label: '文档'
      }
    ],
    currentTab: 'article',
    searchRes: {
      article: [],
      user: [],
      searchMore: ''
    }
  }),
  actions: {
    async selectList(parms: selectList.IParms) {
      const res = await selectListApi(parms)
      if (iscode(res)) {
        const { list, user } = res.result
        this.searchRes.article = list ?? []
        this.searchRes.user = user ?? []
      }
    }
  },
  persist: {
    storage: localStorage,
    paths: ['tabs', 'currentTab']
  }
})

export default useTopbarStore
